<?php
/* $Id$ */
/*
	firewall_mac_edit.php

	originally part of stairway
	Copyright (C) 2009 Wang Zhongliang <wzlsh629@163.com>.
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

##|+PRIV
##|*IDENT=page-firewall-mac-edit
##|*NAME=Firewall: Mac edit page
##|*DESCR=Allow access to the 'Firewall: Mac edit' page.
##|*MATCH=firewall_mac_edit.php*
##|-PRIV


require("guiconfig.inc");

$lang = array('pass' => '通过', 'block' => '阻止');

if (!is_array($config['filter']['macrule'])) {
	$config['filter']['macrule'] = array();
}

$a_filter = &$config['filter']['macrule'];

$id = $_GET['id'];
if (is_numeric($_POST['id']))
	$id = $_POST['id'];

if (isset($id) && $a_filter[$id]) {

	if (!isset($a_filter[$id]['type']))
		$pconfig['type'] = "block";
	else
		$pconfig['type'] = $a_filter[$id]['type'];

	$pconfig['disabled'] = isset($a_filter[$id]['disabled']);
	$pconfig['address'] = $a_filter[$id]['address'];
	$pconfig['descr'] = $a_filter[$id]['descr'];

} else {

	$pconfig['type'] = $config['filter']['macdefault'] == "block" ? "pass" : "block";

}

if ($_POST) {

	$pconfig = $_POST;

	/*  run through $_POST items encoding HTML entties so that the user
	 *  cannot think he is slick and perform a XSS attack on the unwilling 
	 */
//	foreach ($_POST as $key => $value) {
//		$temp = $value;
//		$newpost = htmlentities($temp);
//		if($newpost <> $temp)   	 
//			$input_errors[] = "Invalid characters detected ($temp).  Please remove invalid characters and save again.";		
//	}

	/* input validation */
	$reqdfields = array("address");
	$reqdfieldsn = array("MAC地址");
	
	if(!is_macaddr($_POST['address']))
	   $input_errors[] = "请输入合法的MAC地址";

	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);

	if (!$input_errors) {
		$filterent = array();
		$filterent['type'] = $_POST['type'];
		$filterent['address'] = $_POST['address'];
		//strncpy($filterent['descr'], $_POST['descr'], 52);
		$filterent['descr'] = mb_substr($_POST['descr'], 0, 60, 'utf-8');
        if ($_POST['disabled'])
            $filterent['disabled'] = true;
        else
            unset($filterent['disabled']);
		if (isset($id) && $a_filter[$id])
			$a_filter[$id] = $filterent;
		else {
			$a_filter[] = $filterent;
		}
		write_config();
		touch($d_macconfdirty_path);

		header("Location: firewall_mac.php");

	}
}

$pgtitle = array("防火墙","MAC过滤","编辑");
$pgmenu = array('firewall', '/firewall_mac.php');
$closehead = false;

$page_filename = "firewall_mac_edit.php";
include("head.inc");

?>

</head>

<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>
<?php if ($input_errors) print_input_errors($input_errors); ?>

<form action="firewall_mac_edit.php" method="post" name="iform" id="iform">
	<table width="100%" border="0" cellpadding="6" cellspacing="0">
		<tr>
			<td colspan="2" valign="top" class="listtopic">编辑MAC过滤规则</td>
		</tr>	
    	<tr>
			<td width="22%" valign="top" class="vncellreq">操作</td>
			<td width="78%" class="vtable">
				<input type="hidden" name="type" value="block">阻止
				<!--<select name="type" class="formselect">
					<?php $types = explode(" ", "Pass Block"); foreach ($types as $type): ?>
					<option value="<?=strtolower($type);?>" <?php if (strtolower($type) == strtolower($pconfig['type'])) echo "selected"; ?>>
					<?=$lang[strtolower($type)];?>
					</option>
					<?php endforeach; ?>
				</select>
				<br/>
				<span class="vexpl">
					Choose what to do with packets that match the criteria specified below. <br/>
					Hint: the difference between block and reject is that with reject, a packet (TCP RST or ICMP port unreachable for UDP) is returned to the sender, whereas with block the packet is dropped silently. In either case, the original packet is discarded. Reject only works when the protocol is set to either TCP or UDP (but not &quot;TCP/UDP&quot;) below.
				</span>-->
			</td>
		</tr>
		<tr>
			<td width="22%" valign="top" class="vncellreq">状态</td>
			<td width="78%" class="vtable">
			    <select name="disabled" class="formselect" id="disabled">
			    <option value="" >生效</option>
			    <option value="yes" <?php if ($pconfig['disabled']) echo "selected"; ?>>无效</option>
			    </select>
			</td>
		</tr>
		<tr>
			<td width="22%" valign="top" class="vncellreq">局域网MAC地址</td>
			<td width="78%" class="vtable">
				<input name="address" type="text" class="formfld unknown" id="address" size="30" value="<?php if (!is_specialnet($pconfig['address'])) echo htmlspecialchars($pconfig['address']);?>">
			</td>
		</tr>
		<tr>
			<td width="22%" valign="top" class="vncell">描述</td>
			<td width="78%" class="vtable">
				<input name="descr" type="text" class="formfld unknown" id="descr" size="52" maxlength="52" value="<?=htmlspecialchars($pconfig['descr']);?>">
				<br />
				<span class="vexpl">您可以输入一些描述作为参考。</span>
			</td>
		</tr>
		<tr>
			<td width="22%" valign="top">&nbsp;</td>
			<td width="78%">
				<input name="Submit" type="submit" class="formbtn" value="保存">  <input type="button" class="formbtn" value="取消" onclick="history.back()">
<?php			if (isset($id) && $a_filter[$id]): ?>
					<input name="id" type="hidden" value="<?=$id;?>">
<?php 			endif; ?>
			</td>
		</tr>
	</table>
</form>
<?php include("fend.inc"); ?>
</body>
</html>

